package com.cwl.pattern.享元模式;

import java.util.HashMap;

/**
 * @program: pattern
 * @description: 享元工厂
 * @author: ChenWenLong
 * @create: 2019-06-25 09:13
 **/
public class FlyweightFactory {

    private HashMap<String, Flyweight> flyweights=new HashMap<String, Flyweight>();

    public Flyweight getFlyweight(String key) {

        Flyweight flyweight=(Flyweight)flyweights.get(key);
        if(flyweight != null) {
            System.out.println("具体享元"+key+"已经存在，被成功获取！");
        } else {
            flyweight=new ConcreteFlyweight(key);
            flyweights.put(key, flyweight);
        }
        return flyweight;
    }
}
